package com.ruoyi.ruoyiyinzheng.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.ruoyiyinzheng.domain.bo.StMaterialCategeryBo;
import com.ruoyi.ruoyiyinzheng.service.IStMaterialCategeryService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.ruoyiyinzheng.domain.vo.StMaterialVo;
import com.ruoyi.ruoyiyinzheng.domain.bo.StMaterialBo;
import com.ruoyi.ruoyiyinzheng.service.IStMaterialService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 物料管理控制器
 * 前端访问路由地址为:/ruoyiyinzheng/material
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/material")
public class StMaterialController extends BaseController {
    private final IStMaterialCategeryService iStMaterialCategeryService;

    private final IStMaterialService iStMaterialService;

    /**
     * 查询物料管理列表
     */
    @SaCheckPermission("ruoyiyinzheng:material:list")
    @GetMapping("/list")
    public TableDataInfo<StMaterialVo> list(StMaterialBo bo, PageQuery pageQuery) {
        return iStMaterialService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出物料管理列表
     */
    @SaCheckPermission("ruoyiyinzheng:material:export")
    @Log(title = "物料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StMaterialBo bo, HttpServletResponse response) {
        List<StMaterialVo> list = iStMaterialService.queryList(bo);
        ExcelUtil.exportExcel(list, "物料管理", StMaterialVo.class, response);
    }

    /**
     * 获取物料管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("ruoyiyinzheng:material:query")
    @GetMapping("/{id}")
    public R<StMaterialVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iStMaterialService.queryById(id));
    }

    /**
     * 新增物料管理
     */
    @SaCheckPermission("ruoyiyinzheng:material:add")
    @Log(title = "物料管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StMaterialBo bo) {
        return toAjax(iStMaterialService.insertByBo(bo));
    }

    /**
     * 修改物料管理
     */
    @SaCheckPermission("ruoyiyinzheng:material:edit")
    @Log(title = "物料管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StMaterialBo bo) {
        return toAjax(iStMaterialService.updateByBo(bo));
    }

    /**
     * 删除物料管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("ruoyiyinzheng:material:remove")
    @Log(title = "物料管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iStMaterialService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 获取部门树列表
     */
    @SaCheckPermission("ruoyiyinzheng:material:list")
    @GetMapping("/categeryTree")
    public R<List<Tree<Long>>> deptTree(StMaterialCategeryBo stMaterialCategerybo) {
        return R.ok(iStMaterialCategeryService.selectMaterialCategeryreeList(stMaterialCategerybo));
    }
}
